package com.opi.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.opi.util.CommUtil;
import com.opi.util.ConnDB;
import com.opi.util.ReadScript;

public class CommAction extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(CommAction.class);
	private String password = "199432";
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ConnDB.getConn();
		String method = "";
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		method = request.getParameter("m");
		if(method!=null){
			//关机和重启
		    String command = "";
		    if(method.equals("shutdown")){
		    	command = "shutdown 0";
		    	String pass = request.getParameter("pass");
		    	logger.info("准备关机==="+command);
		    	if(pass!=null && pass.equals(password)){
		    		CommUtil.sendCmd(command);
		    		out.write("true");
		    	}else{
		    		out.write("error");
		    	}
		    }else  if(method.equals("reboot")){
		    	command = "reboot";
		    	String pass = request.getParameter("pass");
		    	logger.info("准备重启==="+command);
		    	if(pass!=null && pass.equals(password)){
		    		CommUtil.sendCmd(command);
		    		out.write("true");
		    	}else{
		    		out.write("error");
		    	}
		    }else if(method.equals("temp")){
		    	//监控CPU温度
				command = "cat /sys/devices/virtual/thermal/thermal_zone0/temp";
				String resp = CommUtil.getCpuTempOrLoadInfo(command);
				logger.info("获取cpu温度===" +resp);
				out.write(resp);
				//TODO test
				/*
				String rand = Math.random()*55+30+"";
				out.write(rand);
				*/
				
			}else if(method.equals("cpu")){
				//监控CPU
				command = ReadScript.getCpuMonitor();
				String resp = CommUtil.getCpuTempOrLoadInfo(command);
				logger.info("获取cpu使用率==="+resp);
				out.write(resp);
				
			}else if(method.equals("free")){
				//监控内存使用率
				command = "free -m";
				String str = CommUtil.getDiskOrMemoryInfo(command);
				logger.info("获取内存信息==="+str);
				str  =  str.replaceAll("\\s", "&nbsp;");
				//out.write("OK");
				
				//TODO test
				/*
				String str = "             total       used       free     shared    buffers     cached<br>";
				str+=	"Mem:          2013       1853        160         14         24       1665<br> ";
				str+=	"-/+ buffers/cache:        162       1850<br>";
				str+=	"Swap:            0          0          0<br>";
				str  =  str.replaceAll("\\s", "&nbsp;");
				*/
				if(str.contains("total")){
					str  =  str.replace("total", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;总计");
				}
				if(str.contains("used")){
					str  =  str.replace("used", "已 用");
				}
				if(str.contains("free")){
					str  =  str.replace("free", "空 闲");
				}
				if(str.contains("shared")){
					str  =  str.replace("shared", "共 享");
				}
				if(str.contains("cached")){
					str  =  str.replace("cached", "缓 冲");
				}
				if(str.contains("Mem")){
					str  =  str.replace("Mem", "&nbsp;&nbsp;存贮器");
				}
				if(str.contains("buffers/cache")){
					str  =  str.replace("-/+ buffers/cache", "缓冲/缓存");
				}
				if(str.contains("Swap")){
					str  =  str.replace("Swap", "虚拟内存");
				}
				out.write(str);
				
			}else if(method.equals("disk")){
				//监控磁盘状态
				command = "df -h";
				String s = CommUtil.getDiskOrMemoryInfo(command);
				logger.info("获取磁盘信息==="+s);
				s  = s.replaceAll("\\s", "&nbsp;&nbsp;");
				
				//TODO test
				/*
				String s = "Filesystem      Size  Used Avail Use% Mounted on<br>";
				 	s += "/dev/mmcblk0p2   15G  7.0G  6.6G  52% /<br>";
				    s += "udev             10M     0   10M   0% /dev<br>";
					s += "tmpfs           403M   11M  392M   3% /run<br>";
					s += "tmpfs          1007M     0 1007M   0% /dev/shm<br>";
					s  =  s.replaceAll("\\s", "&nbsp;&nbsp;");
					*/
					
					if(s.contains("Filesystem")){
						s  =  s.replace("Filesystem", "文件系统");
					}
					if(s.contains("Size")){
						s  = s.replace("Size", "&nbsp;&nbsp;&nbsp;&nbsp;大小");
					}
					if(s.contains("Used")){
						s  = s.replace("Used", "&nbsp;&nbsp;已用");
					}
					if(s.contains("Avail")){
						s  = s.replace("Avail", "&nbsp;&nbsp;可用");
					}
					if(s.contains("Use%")){
						s  = s.replace("Use%", "&nbsp;&nbsp;使用率");
					}
					if(s.contains("Mounted")){
						s  = s.replace("Mounted", "&nbsp;挂载点");
					}
				out.write(s);
				
			}else if(method.equals("net")){
				//监控网络状态
				command = ReadScript.getNetMonitor();
				String json = CommUtil.getNetInfo(command);
				out.write(json);
			}else{
				logger.error("m:参数有误！");
			}
		}
		
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
